"""
用户账号模型（对应现有 mx_user_info 表）
"""
from datetime import datetime
from app import db


class UserInfo(db.Model):
    """用户账号表（对应现有 mx_user_info 表）"""
    __tablename__ = 'mx_user_info'
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(100), nullable=False, comment='昵称')
    account = db.Column(db.String(50), nullable=False, comment='账号')
    password = db.Column(db.String(50), nullable=False, comment='密码')
    role_id = db.Column(db.Integer, comment='角色id（控制后台菜单权限）')
    region_company = db.Column(db.String(1200), comment='公司/区域')
    company = db.Column(db.Text, comment='开票公司')
    create_time = db.Column(db.DateTime, default=datetime.utcnow, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment='更新时间')
    wx_openid = db.Column(db.String(100), comment='WeChat OpenID')
    staff_status = db.Column(db.Enum('待审核', '正常'), default='正常', comment='Staff status')
    avatar_url = db.Column(db.String(255), comment='Avatar URL')
    wx_access_token = db.Column(db.String(255), comment='WeChat access token')
    phone = db.Column(db.String(50), comment='手机号')
    
    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'account': self.account,
            'role_id': self.role_id,
            'company': self.company,
            'phone': self.phone,
            'wx_openid': self.wx_openid,
            'staff_status': self.staff_status,
            'avatar_url': self.avatar_url,
            'create_time': self.create_time.isoformat() if self.create_time else None,
            'update_time': self.update_time.isoformat() if self.update_time else None
        }
